import json
import Robotics

def init():
    # ================ 全局变量定义 ================#
    global clientsockets  # 保存客户端socket
    global client_addrs  # 保存客户端addr
    global Robot_list  # 保存智能体对象
    global client_num  # 客户端连接数量
    global connect_change  # 客户端数量变化标志
    global Thread_recv_send_list  # 客户端收发数据线程列表
    global Send_data  # 发送数据缓冲区

    clientsockets = []
    client_addrs = []
    Robot_list = []
    client_num = 0
    connect_change = False
    Thread_recv_send_list = []
    Send_data = {
        "header": "",
        "start_x": 0.0,
        "start_y": 0.0
    }

# ================ 客户端 clientsockets 操作 ================#
# 1.按索引删除客户端socket
def del_clientsockets(index):
    clientsockets.pop(index)

# 2.添加客户端socket
def append_clientsockets(client):
    clientsockets.append(client)

# 3.关闭客户端socket
def close_clientsockets(index):
    clientsockets[index].close()

# 4.清空客户端socket
def clear_clientsockets():
    clientsockets.clear()

# 5.向客户端socket发送数据
def send_clientsockets(index,Send_data):
    clientsockets[index].send(json.dumps(Send_data).encode())

# 6.获取客户端socket列表
def get_clientsockets():
    return clientsockets

# 7.获取某个客户端socket
def get_one_clientsockets(index):
    return clientsockets[index]


# ================ 客户端 client_addrs 操作 ================#
# 1.获取客户端addr列表
def get_client_addrs():
    return client_addrs

# 2.获取某个客户端addr的索引
def get_index_client_addrs(addr):
    return client_addrs.index(addr)

# 3.删除客户端addr
def del_client_addrs(index):
    client_addrs.pop(index)

# 4.添加客户端addr
def append_client_addrs(addr):
    client_addrs.append(addr)

# 5.清空客户端addr
def clear_client_addrs():
    client_addrs.clear()


# ================ 智能体数量 client_num 操作 ================#
# 1.获取客户端连接数量
def get_client_num():
    return client_num

# 2.设置客户端连接数量
def set_client_num(num):
    global client_num
    client_num = num


# ================ 客户端数量变化 connect_change 操作 ================#
# 1.获取客户端数量变化标志
def get_connect_change():
    return connect_change

# 2.设置客户端数量变化标志
def set_connect_change(re):
    global connect_change
    connect_change = re


# ================ 智能体对象操作 ================#
# 1.获取智能体对象列表
def get_Robot_list():
    return Robot_list

# 2.获取某个智能体对象
def get_one_Robot_list(index):
    return Robot_list[index]

# 3.删除智能体对象
def del_Robot_list(index):
    Robot_list.pop(index)

# 4.添加智能体对象
def append_Robot_list(robot):
    Robot_list.append(robot)


# ================ 客户端收发数据线程 Thread_recv_send_list 操作 ================#
# 1.获取客户端收发数据线程列表
def get_Thread_recv_send_list():
    return Thread_recv_send_list

# 2.获取某个客户端收发数据线程
def get_one_Thread_recv_send_list(index):
    return Thread_recv_send_list[index]

# 3.添加客户端收发数据线程
def append_Thread_recv_send_list(thread):
    Thread_recv_send_list.append(thread)

# 4.删除客户端收发数据线程
def del_Thread_recv_send_list(index):
    Thread_recv_send_list.pop(index)

# 5.清空客户端收发数据线程列表
def clear_Thread_recv_send_list():
    Thread_recv_send_list.clear()



# ================ 数据缓冲区 Send_data 操作 ================#
# 获取发送数据缓冲区
def get_Send_data():
    return Send_data

# 设置发送数据缓冲区中字典的键值
def set_Send_data(arg,value):
    Send_data[arg] = value
